package com.xuecheng.basic.controller;

import cn.hutool.core.date.DateUtil;
import com.xuecheng.basic.domain.Dictionary;
import com.xuecheng.basic.service.DictionaryService;
import com.xuecheng.commons.model.vo.DictionaryVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 数据字典前端控制器
 */
@Slf4j
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService  dictionaryService;

    /**
     * 查询所有数据字典
     * 请求路径：GET
     * 响应数据ResponseResult<List<VO>>
     */
    @GetMapping
    public ResponseResult<List<DictionaryVo>> findAll() {
        log.info("查询所有:{}"+ DateUtil.now());
        //1、调用service查询所有：Dictionary
        List<Dictionary> list = dictionaryService.list();
        //2、转化
        List<DictionaryVo> vos = BeanHelper.copyWithCollection(list, DictionaryVo.class);
        //3、返回
        return ResponseResult.okResult(vos);
    }

    /**
     * 根据code查询数据字典
     * 请求路径：GET：/{code}
     * 请求参数：查询条件code
     * 响应数据 ResponseResult<DictionaryVo>
     */
    @GetMapping("/{code}")
    public ResponseResult<DictionaryVo> findByCode(@PathVariable("code") String code) {
        return dictionaryService.findByCode(code);
    }
}
